import { BattlePhaseMachine } from "@/Battle/StateMechine/BattlePhaseMachine.js";
import { RoundMachine } from "@/Battle/StateMechine/RoundMachine.js";
import { TurnMechine } from "@/Battle/StateMechine/TurnMachine.js";
import { ActionMachine } from "@/Battle/StateMechine/ActionMachine.js";

export class MachineCoordinator{
    constructor(){
        this.machines=[
            new BattlePhaseMachine(),
            new RoundMachine(),
            new TurnMechine(),
            new ActionMachine()
        ];
        this.index=0;

        this.runing=true;
    }

    start(){
        this.machines[this.index].start();
    }

    update(){
        if(!this.runing)return;
        let machine=this.machines[this.index];
        if(!machine.runing){
            if(machine.isback()){
                --this.index;
                if(this.index<0){
                    this.runing=false;
                    return;
                }
                machine=this.machines[this.index];
                machine.restore();
            }else if(machine.isnext()){
                ++this.index;
                if(this.index>=this.machines.length){
                    this.runing=false;
                    return;
                }
                machine=this.machines[this.index];
                machine.start();
            }else{
                machine.restore();
            }
        }

        machine.update();
    }
    
    clear(){
        for(let machine of this.machines){
            machine.clear();
        }
        this.machines.length=0;
    }
}